package com.gxuwz.weatherapp;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.viewpager2.widget.ViewPager2;

public class WeatherPageTransformer implements ViewPager2.PageTransformer {
    private static final float MIN_SCALE = 0.85f;
    private static final float MIN_ALPHA = 0.5f;

    @Override
    public void transformPage(@NonNull View page, float position) {
        int pageWidth = page.getWidth();
        int pageHeight = page.getHeight();

        if (position < -1) { // [-Infinity,-1)
            // 页面已经在屏幕左侧完全不可见
            page.setAlpha(0f);
            page.setTranslationX(0f);
        } else if (position <= 1) { // [-1,1]
            // 页面在屏幕上可见或部分可见

            // 缩放效果
            float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
            float vertMargin = pageHeight * (1 - scaleFactor) / 2;
            float horzMargin = pageWidth * (1 - scaleFactor) / 2;

            // 将页面移动到中心位置
            if (position < 0) {
                // 向左滑动
                page.setTranslationX(horzMargin - vertMargin / 2);
            } else {
                // 向右滑动
                page.setTranslationX(-horzMargin + vertMargin / 2);
            }

            // 根据位置缩放页面
            page.setScaleX(scaleFactor);
            page.setScaleY(scaleFactor);

            // 根据位置淡化页面
            page.setAlpha(MIN_ALPHA + (scaleFactor - MIN_SCALE) / (1 - MIN_SCALE) * (1 - MIN_ALPHA));

        } else { // (1,+Infinity]
            // 页面已经在屏幕右侧完全不可见
            page.setAlpha(0f);
            page.setTranslationX(0f);
        }
    }
} 